//
//  TextFieldView.m
//  CoolFM
//
//  Created by 曹曹 on 16/8/29.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "TextFieldView.h"
#import "UIView+Frame.h"

@interface TextFieldView () <UITextFieldDelegate>

@end

@implementation TextFieldView

- (instancetype)initWithFrame:(CGRect)frame image:(NSString *)imageName placeHolder:(NSString *)placeHolder secureTextEntry:(BOOL)secure {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUpWithImage:imageName placeHolder:placeHolder secureTextEntry:secure];
    }
    return self;
}

+ (instancetype)textFieldWithFrame:(CGRect)frame image:(NSString *)imageName placeHolder:(NSString *)placeHolder secureTextEntry:(BOOL)secure {
    return [[self alloc] initWithFrame:frame image:imageName placeHolder:placeHolder secureTextEntry:secure];
}

- (void)setUpWithImage:(NSString *)imageName placeHolder:(NSString *)placeHolder secureTextEntry:(BOOL)secure {
    CGFloat margin = self.gx_height / 3;
    CGFloat imageH = self.gx_height / 1.5;
    CGFloat imageW = imageH;
    CGRect imageRect = CGRectMake(margin / 2, 0, imageW, imageH);
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageRect];
    imageView.gx_centerY = self.gx_centerY;
    imageView.image = [UIImage imageNamed:imageName];
    [self addSubview:imageView];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(imageW + margin, 0, self.gx_width - imageW, self.gx_height)];
    textField.placeholder = placeHolder;
    [textField setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    textField.secureTextEntry = secure;
    [self addSubview:textField];
    textField.delegate = self;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField endEditing:YES];
    return YES;
}

@end
